package com.ideal.manage.esms.service.system;

import com.ideal.manage.esms.bean.project.ProjectManage;
import com.ideal.manage.esms.bean.sys.User;
import com.ideal.manage.esms.bean.sys.UserProject;
import com.ideal.manage.esms.repository.system.UserProjectRepository;
import com.ideal.manage.esms.repository.system.UserRepository;
import com.ideal.manage.esms.service.project.ProjectManageService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class UserProjectService {
    @Autowired
    private UserRepository userRepository;
    @Autowired
    private RoleService roleService;
    @Autowired
    private CustomerService customerService;
    @Resource
    private ProjectManageService projectManageService;
    @Resource
    private UserProjectRepository userProjectRepository;

    public void saveUserProject(User user, String objectIds){
        List<UserProject> userProjects=new ArrayList<>();
        List<UserProject> delUserProjects=new ArrayList<>();
        if(!StringUtils.isBlank(objectIds)){
            if(objectIds.contains(",")){
                String[] ids=objectIds.split(",");
                for(int i=0;i<ids.length;i++){
                    //先删除原来的关联关系
                    delUserProjects=userProjectRepository.findProjects(user.getId());
                    if(delUserProjects.size()>0){
                        for(UserProject userProject:delUserProjects){
                            userProject.setIsValid(0);
                            userProjectRepository.save(userProject);
                        }
                    }
                    //新增新的权限项目关系
                    ProjectManage projectManage=projectManageService.findOne(ids[i]);
                    if(projectManage!=null){
                        UserProject userProject=new UserProject();
                        userProject.setUserId(user);
                        userProject.setIsValid(1);
                        userProject.setProjectId(projectManage);
                        userProjects.add(userProject);
                    }
                }
            }else{
                //先删除原来的关联关系
                delUserProjects=userProjectRepository.findProjects(user.getId());
                if(delUserProjects.size()>0){
                    for(UserProject userProject:delUserProjects){
                        userProject.setIsValid(0);
                        userProjectRepository.save(userProject);
                    }
                }
                //新增新的权限项目关系
                ProjectManage projectManage=projectManageService.findOne(objectIds);
                if(projectManage!=null){
                    UserProject userProject=new UserProject();
                    userProject.setUserId(user);
                    userProject.setIsValid(1);
                    userProject.setProjectId(projectManage);
                    userProjects.add(userProject);
                }
            }
        }else{
            List<UserProject> userProjects1=userProjectRepository.findProjects(user.getId());
            for(UserProject userProject:userProjects1){
                userProject.setIsValid(0);
                userProjects.add(userProject);
            }
        }
        userProjectRepository.save(userProjects);
    }

    public List<UserProject> findProjects(String id){
        List<UserProject> userProjects=userProjectRepository.findProjects(id);
        return userProjects;
    }

    public void delUserProjects(String[] ids) {
        List<UserProject> newUserProjects = new ArrayList<>();
        for (int i = 0; i < ids.length; i++) {
            List<UserProject> userProjects = userProjectRepository.findByProjectId(ids[i]);
            for (UserProject userProject : userProjects) {
                userProject.setIsValid(0);
                newUserProjects.add(userProject);
            }
            userProjectRepository.save(newUserProjects);
        }
    }
}
